package test;

import java.util.ArrayList;
import java.util.List;

import lib.dbTool.*;//修正 lib.
import test.*;
import my.item.*;
import my.gridView.*;

public class StudentTableBean extends GridModel {
	
	//錯誤訊息
	private List<String> err;
	//更新訊息
	private String updateState;
	//刪除訊息
	private String deleteState;
	
	
	private int showAlert;

	public int getShowAlert() {
		return showAlert;
	}

	public void setShowAlert(int showAlert) {
		this.showAlert = showAlert;
	}

	public String getDeleteStata() {
		return deleteState;
	}

	public void setDeleteStata(String deleteStata) {
		this.deleteState = deleteStata;
	}

	public String getUpdateState() {
		return updateState;
	}

	public void setUpdateState(String updateState) {
		this.updateState = updateState;
	}

	public List<String> getErr() {
		return err;
	}

	public void setErr(List<String> err) {
		this.err = err;
	}

	public StudentTableBean() {
		super(new DefaultJNumberList());
	}

	// ---------------------------------
	private String idstudent;

	public String getIdstudent() {
		return idstudent;
	}

	public void setIdstudent(String idstudent) {
		this.idstudent = idstudent;
	}

	// ---------------------------------
	private String name;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	// ---------------------------------
	private String phone;

	public String getPhone() {
		return phone;
	}

	public void setPhone(String phone) {
		this.phone = phone;
	}

	// ---------------------------------
	// ---------------------------------
	private String nameAdd;

	public String getNameAdd() {
		return nameAdd;
	}

	public void setNameAdd(String nameAdd) {
		this.nameAdd = nameAdd;
	}

	// ---------------------------------
	private String phoneAdd;

	public String getPhoneAdd() {
		return phoneAdd;
	}

	public void setPhoneAdd(String phoneAdd) {
		this.phoneAdd = phoneAdd;
	}

	// ---------------------------------

	/**
	 * 自訂事件
	 */
	@Override
	public void jaddListener() {
		// 指定PK 修正
		this.idstudent = pk;
		// 新增一筆資料利用 【自訂事件】
		//2013-12-31 自訂自己的事件
		this.addJMyListener(new JMyListener() {

			@Override
			public String getMyCommandValue() {
				return "qkadd";
			}

			@Override
			public void action(GridModel gridModel) {
				System.out.println("自訂新增被按了");
				//qkadd();
				//jadd();
			}
			
		});
		this.addJMyListener(new JMyListener() {
			
			@Override
			public String getMyCommandValue() {
				// TODO Auto-generated method stub
				return "myButton1";
			}
			
			@Override
			public void action(GridModel gridModel) {
				System.out.println("myButton1 被按了 , PK=" + pk);
			}
		});
		
		this.addJMyListener(new JMyListener() {
			
			@Override
			public String getMyCommandValue() {
				// TODO Auto-generated method stub
				return "edit_myButton1";
			}
			
			@Override
			public void action(GridModel gridModel) {
				System.out.println("進入 edit mode , PK=" + pk);
			}
		});
	}

	@Override
	public void jedit() {

	}

	@Override
	public void jdel() {
		STUDENT x = new STUDENT();
		int int_idstudent = Integer.parseInt(pk);
		int idstudentInt = Integer.parseInt(idstudent);
		int row = x.delete(idstudentInt);
		if(row>0){
			showAlert =1;
		}else{
			showAlert =0;
		}
	}

	@Override
	public void jyes() {
		STUDENT x = new STUDENT();
		int int_idstudent = Integer.parseInt(pk);
		int idstudentInt = Integer.parseInt(idstudent);
		int row = x.update(name, phone, idstudentInt);
		if(row==1){
			updateState = name+"更新一筆成功";
		}else{
			updateState = name+"無更新";
		}
	}

	@Override
	public void jno() {

	}

	@Override
	public void jadd() {
		STUDENT x = new STUDENT();
		int row = x.insert(nameAdd, phoneAdd);
		System.out.println("新增結果 = " + row);
		if(row==0){
			err = new ArrayList<String>();
			err.add("新增錯誤");
			System.out.println("錯誤="+err);
			return;
		}
		// 新增一筆後, 要重新計算頁數, 然後再指定到最後一頁
		totalPage();
		this.toPage = this.totalPage;
	}

	@Override
	public void sortData(String fieldName, int sortNowValue) {
		// TODO Auto-generated method stub

	}

	/**
	 * 找出共有幾頁
	 */
	@Override
	public void totalPage() {
		// TODO Auto-generated method stub
		STUDENT x = new STUDENT();
		rowCount = x.selectCount();
		totalPage = rowCount / pageSize;
		totalPage += rowCount % pageSize == 0 ? 0 : 1;
	}

	/**
	 * select 分頁出來的內容
	 */
	@Override
	public TableData selectPage(int pageSize, int pageIndex) {
		// TODO Auto-generated method stub
		STUDENT x = new STUDENT();
		// ------------新增
		if (pageIndex <= 0) {
			pageIndex = 1;
		}

		// this.sortNowName : 我按到哪一欄位
		// this.sortNowValue : 排序方向
		// 0=不排序, 1=正向, 2=反向

		String db[][] = null;
		String sortName;
		int desc;

		// 以下處理排序問題
		sortName = this.sortNowName;
		if (this.sortNowValue == 0) {
			db = x.selectPage(pageSize, pageIndex, rowCount);
		} else {
			if (this.sortNowValue == 1) {
				desc = PaggingSQL.ASC;
			} else {
				desc = PaggingSQL.DESC;
			}
			db = x.selectPageOrderBy(pageSize, pageIndex, sortName, desc,
					rowCount);
		}
		// ------------新增
		TableData t = new TableData();
		// keyIndex 要自己根據 db 來決定
		int keyIndex = 0;
		// fake 為 db select 出來的 String[][]
		t.setData(keyIndex, db);
		return t;
	}

	/**
	 * 非編輯模式的資料
	 */
	@Override
	public Cell[] loadLabel(TableData t, TempCell tc) {
		// TODO Auto-generated method stub
		String datas[][] = t.getDatas();
		// 表示select db 出來的欄位數量
		int colSize = 3;
		Cell tmpCell[] = new Cell[colSize];
		// 第一欄預設為 [CLabel], 若修改為其它的 class 請參考 [CLabel], showYesNo() = false
		// getValue() 要傳回值[純PK],不可有<input>,第一欄一定為 PK
		// 若不想 show 出來, getHtmlItem()可以在control.jsp 中刪除
		tmpCell[0] = new CLabel("idstudent");
		tmpCell[1] = new CLabel("name");
		tmpCell[2] = new CLabel("phone");

		tc.loadCell(tmpCell, datas);
		return tmpCell;
	}

	/**
	 * 進入編輯模式的資料
	 */
	@Override
	public Cell[] loadItem(TableData t, TempCell tc) {
		// TODO Auto-generated method stub
		String datas[][] = t.getDatas();
		int colSize = 3;
		Cell tmpCell[] = new Cell[colSize];
		// 第一欄預設為 [CLabelForUpdate][CLabelHidden], 若修改為其它的 class 請參考
		// [CLabelForUpdate][CLabelHidden]
		// , showYesNo() = true, getValue() 要傳回值[純PK],不可有<input>, 第一欄一定為 PK
		// 若不想 show 出來, getHtmlItem()可以在control.jsp 中刪除
		tmpCell[0] = new CLabelForUpdate("idstudent");
		tmpCell[1] = new CText("name");
		tmpCell[2] = new CText("phone");

		tc.loadCell(tmpCell, datas);
		return tmpCell;
	}

}
